package exam.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/3/31 11:29
 * @since JDK 1.8
 *
 * 本地数据测试Demo
 */
public class TestDemo {

    private static final String suffix=".data";

    private static final String PATH=System.getProperty("user.dir");

    private static final String dataPath="/localdata";

    private static final String[] strings={"DGroupPreview","DLevelPreview","DPointPreview"};


    public static void main(String[] args) {
        Test62();
    }


    public static void Test62(){
        JSONArray jsonArray=new JSONArray();
        String s=null;
        for (int i = 0; i < 10 ; i++) {
            JSONObject jsonObject=new JSONObject();
            if(i%2==0){
               s="DGroupPreview";
            }else {
                s="111111";
            }
            jsonObject.put("preview",s);
            jsonArray.add(jsonObject);
        }
        jsonArray.forEach(e->{
            JSONObject jsonObject=(JSONObject) e;
            System.out.println(jsonObject.get("preview"));
        });
        System.out.println("==========================");

        List<String> stringList= Arrays.asList(strings);
        Iterator iterator=jsonArray.iterator();
        while (iterator.hasNext()){
            JSONObject jsonObject=(JSONObject)iterator.next();
            if(!jsonObject.containsKey("preview")){
                continue;
            }
            String preview=jsonObject.getString("preview");
            boolean flag=stringList.contains(preview);
            if(flag){
                iterator.remove();
            }
        }
        jsonArray.forEach(e->{
            JSONObject jsonObject=(JSONObject) e;
            System.out.println(jsonObject.get("preview"));
        });

    }

    public static void Test61(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String str=simpleDateFormat.format(new Date());
        str+=(int)((Math.random()*9+1)*10000000);
        System.out.println(str);
    }


    public static void Test51(){
        StringJoiner stringJoiner=new StringJoiner(",");
        for (int i = 0; i < 100; i++) {
            stringJoiner.add(String.valueOf(i));
        }
        System.out.println(stringJoiner);
        String s="123456";
        String.format("用户信息为空 %s", s);
        List names=new ArrayList<String>();
        names.add("1");
        names.add("2");
        names.add("3");
        System.out.println(String.join("-", names));
        String[] arrStr=new String[]{"a","b","c"};
        System.out.println(String.join("-", arrStr));
    }

    public static void Test50(){
        //File file = new File("D:\\code\\spring-demo\\sql.log");
        //System.out.println(txt2String(file));
        try {
            File file = new File("D:\\code\\spring-demo\\sql.log");
            String str=txt2String(file);
            writeTxtFile(str);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String string = null;
            int i=0;
            while((string = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(i).append(System.lineSeparator()).append(string);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 写文件
     *
     * @param newStr
     *      新内容
     * @throws IOException
     */
    public static boolean writeTxtFile(String newStr) throws IOException {
        // 先读取原有文件内容，然后进行写入操作
        boolean flag = false;
        String filein = newStr + "\r\n";
        String temp = "";

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            // 文件路径
            File file = new File("C:\\Users\\xiong\\Desktop\\每日sql\\123.txt");
            // 将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();

            // 保存该文件原有的内容
            for (int j = 1; (temp = br.readLine()) != null; j++) {
                buf = buf.append(temp);
                // System.getProperty("line.separator")
                // 行与行之间的分隔符 相当于“\n”
                buf = buf.append(System.getProperty("line.separator"));
            }
            buf.append(filein);

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            flag = true;
        } catch (IOException e1) {
            // TODO 自动生成 catch 块
            throw e1;
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return flag;
    }



    
    public static void Test49(){
        String s1="yangjie1@huadu.gov.cn,yangjie2@huadu.gov.cn,yangjie3@huadu.gov.cn,yangjie4@huadu.gov.cn,yangjie5@huadu.gov.cn,yangjie6@huadu.gov.cn";
        String[] strings=s1.split(",");
        List<String> list=Arrays.asList(strings);
        List<Test> tests=new ArrayList<>();
        Test test=new Test();
        for (int i = 0; i < list.size() ; i++) {
            Test test1=(Test)test.clone();
            test1.setIndex(i);
            test1.setMsg(list.get(i));
            tests.add(test1);
        }
        for (int i = 0; i < tests.size(); i++) {
            System.out.println(tests.get(i));
        }
        System.out.println("=====================================");

        tests.stream().forEach(e->{
            e.setIndex(e.getIndex()+100);
        });



        tests.forEach(e->{
            System.out.println(e);
        });
    }


    public static void Test48(){
        String s1="yangjie1@huadu.gov.cn,yangjie2@huadu.gov.cn,yangjie3@huadu.gov.cn,yangjie4@huadu.gov.cn,yangjie5@huadu.gov.cn,yangjie6@huadu.gov.cn";
        String[] strings=s1.split(",");
        List<String> list=Arrays.asList(strings);
        StringJoiner stringJoiner=new StringJoiner(" ","","");
        list.stream().forEach(e->{
           stringJoiner.add(e);
           stringJoiner.add(null);
        });
        System.out.println(stringJoiner.toString());
    }

    public static void Test47(){
        System.out.println(String.valueOf((int)((Math.random()*9+1)*100000)));
    }

    public static void Test46(){
        String s="yangjie1@huadu.gov.cn,yangjie2@huadu.gov.cn,yangjie3@huadu.gov.cn";
        String[] strings1=s.split(",");
        List<String> list2=Arrays.asList(strings1);

        String s1="yangjie1@huadu.gov.cn,yangjie2@huadu.gov.cn,yangjie3@huadu.gov.cn,yangjie4@huadu.gov.cn,yangjie5@huadu.gov.cn,yangjie6@huadu.gov.cn";
        String[] strings=s1.split(",");
        List<String> list=Arrays.asList(strings);
        List<Test> tests=new ArrayList<>();
        Test test=new Test();
        for (int i = 0; i < list.size() ; i++) {
            Test test1=(Test)test.clone();
            test1.setIndex(i);
            test1.setMsg(list.get(i));
            tests.add(test1);
        }

        List<Test> stringList3=tests.stream().filter(e->!list2.contains(e.getMsg())).collect(Collectors.toList());
        stringList3.stream().forEach(e->{
            System.out.println(e);
        });

        System.out.println("================================================");

        List<String> stringList1=list.stream().filter(e->!list2.contains(e)).collect(Collectors.toList());
        stringList1.forEach(e->{
            System.out.println(e);
        });
        System.out.println("=============================");
        List<String> stringList=new ArrayList<>(list.size());
        for (int i = 0; i < list.size() ; i++) {
            String str=list.get(i);
            if(!list2.contains(str)){
                stringList.add(str);
            }
        }
        stringList.stream().forEach(e->{
            System.out.println(e);
        });
    }



    public static void Test45(){
        String s="yangjie@huadu.gov.cn,yangjie@huadu.gov.cn,yangjie@huadu.gov.cn,yangjie@huadu.gov.cn,yangjie@huadu.gov.cn,yangjie@huadu.gov.cn";
        String[] strings=s.split(",");
        List<String> list=Arrays.asList(strings);
        arrayReplace(list,"@ak.huadu.gov.cn","@huadu.gov.cn");
        list.stream().forEach(e->{
            System.out.println(e);
        });
    }

    public static void arrayReplace(List<String> strings,String replace,String target){
        for (int i = 0; i < strings.size() ; i++) {
            String str=strings.get(i);
            if(str.contains(target)){
                str=str.replace(target,replace);
            }
            strings.set(i,str);
        }
    }


    public static void test(){
        JSONObject jsonObject= JSON.parseObject("{'code':0,'msg':'success'}");
        System.out.println(
                jsonObject.toString()
        );

        StringJoiner stringJoiner=new StringJoiner("','","('","')");
        for (int i = 0; i < 10; i++) {
            if(i%2==0){
                stringJoiner.add(null);
            }
            stringJoiner.add("ss");
        }
        System.out.println(stringJoiner.toString());  //('null','ss','ss','null','ss','ss','null','ss','ss','null','ss','ss','null','ss','ss')


        String s="yangjie@huadu.gov.cn";
        List<String> list=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(s);
        }

        for (int i = 0; i < list.size() ; i++) {
            String str=list.get(i);
            if(str.contains("@huadu.gov.cn")){
                str=str.replace("@huadu.gov.cn","@ak.huadu.gov.cn");
            }
            list.set(i,str);
        }
    }


    public void FileList(){
        File file = new File(PATH+dataPath);
        String[] file2=file.list();
        for (int i = 0; i < file2.length; i++) {
            System.out.println(file2[i]);
        }

    }


    //获取路径1
    public  void Test(){
        File f = new File(this.getClass().getResource("/").getPath());
        System.out.println(f); //D:\code\spring-demo\SpringBootDemo\target\classes
    }

    //获取路径2
    public void Test2(){
        try {
            File directory = new File("");//参数为空
            String courseFile = directory.getCanonicalPath() ;
            System.out.println(courseFile);  //D:\code\spring-demo
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void Test3(){
        System.out.println(System.getProperty("user.dir")); //D:\code\spring-demo
    }

    public static  void fanxuliehua(String name){
        String nameFile=PATH+dataPath;
        FileInputStream fis=null;
        ObjectInputStream ois=null;
        File file = new File(nameFile+File.separator+"/"+name+suffix);
        try {
            //反序列化
            fis= new FileInputStream(file);
            ois= new ObjectInputStream(fis);
            Map<String, Object> objectMap= ( Map<String, Object>) ois.readObject();
            objectMap.forEach((k,v)->{
                System.out.println(k+"  "+v);
            });
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                ois.close();
            }catch (IOException e2){
                e2.printStackTrace();
            }
        }
    }

    public void mkdirFile(String path){
        File file=new File(path);
    }

    public static void xuliehua(String name){
        String nameFile=PATH+dataPath;
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try {
            File file=new File(nameFile+File.separator+"/"+name+suffix);
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            Map<String, Object> objectMap=new HashMap<>();
            objectMap.put("code",0);
            objectMap.put("msg","success");
            oos.writeObject(objectMap);
            oos.flush();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            }catch (IOException e1){
                e1.printStackTrace();
            }
        }
        //序列化
    }
}
